package com.interfaces;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.microedition.khronos.opengles.GL10;

import com.aybived.main;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.util.Log;

public abstract class AybivedDrawable{
	
	//saves level-tile-images, or images for light effects, background images
	public LinkedList<String> bitmapStrings;
	protected ArrayList<Integer> texIDs;
	protected float vertices[];
	private float uvs[] = {  
							0.0f, -1.0f,
							0.0f, 0.0f,
							1.0f, -1.0f,
							1.0f, 0.0f
						};
	private short index[] = { 0, 1, 2, 3 };
	private ByteBuffer vbb;
	protected FloatBuffer vertexBuffer;
	private ByteBuffer tbb;
	protected FloatBuffer UVBuffer;
	private ByteBuffer ibb;
	private ShortBuffer indexBuffer;
	private int textureWidth;
	private int textureHeight;

	public AybivedDrawable( ) {
		bitmapStrings = new LinkedList<String>();
		texIDs = new ArrayList<Integer>();
	}
	
	public AybivedDrawable( String BitmapString ) {
		bitmapStrings = new LinkedList<String>();
		texIDs = new ArrayList<Integer>();
		bitmapStrings.add( BitmapString );
	}
	
	public AybivedDrawable( LinkedList<String> BitmapStrings ) {
		bitmapStrings = BitmapStrings;
		texIDs = new ArrayList<Integer>();
	}
	
	public void loadGlTexture( GL10 gl, boolean useScreenHeight )
	{int[] tID = new int[bitmapStrings.size()];
		try
		{
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glGenTextures(bitmapStrings.size(), tID, 0);
			
			for(int i=0; i<bitmapStrings.size(); i++)
			{
				Bitmap tmp = BitmapFactory.decodeResource(main.resource, main.resource.getIdentifier( bitmapStrings.get(i), null, null));
				textureWidth = tmp.getWidth();
				textureHeight = tmp.getHeight();
				
				if( useScreenHeight )
					initVertexData( textureWidth, main.getScreenDimesions()[1]);
				else
					initVertexData( textureWidth, textureHeight );
				
				gl.glBindTexture(GL10.GL_TEXTURE_2D, tID[i]);
				GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, tmp, 0);
				gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR );
				gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR );
				texIDs.add( tID[i] );
				tmp.recycle();
			}
		}
		catch( Exception e)
		{
			Log.e( "Error:AybivedDrawable: loadTexture()", e.getMessage() + "| bitmap: " + bitmapStrings.getFirst() + " | texid: " + tID[0] + " | " + gl.glGetString(gl.glGetError()));
		}
	}
	
	
	private void initVertexData( float unitX, float unitY )
	{
		float tempVertices[] = { 	
				0.0f,unitY,0.0f,
				0.0f,0.0f,0.0f,
				unitX,unitY,0.0f,
				unitX,0.0f,0.0f
				};
		vertices = tempVertices;
		
		vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		tbb = ByteBuffer.allocateDirect(uvs.length * 4);
		tbb.order(ByteOrder.nativeOrder());
		
		UVBuffer = tbb.asFloatBuffer();
		UVBuffer.put(uvs);
		UVBuffer.position(0);		
		
		ibb = ByteBuffer.allocateDirect(index.length * 2);
		ibb.order(ByteOrder.nativeOrder());
		
		indexBuffer = ibb.asShortBuffer();
		indexBuffer.put(index);
		indexBuffer.position(0);
	}
	
	public abstract void draw(GL10 gl);

	public void addBitmap( String bitmapString )
	{
		bitmapStrings.add( bitmapString );
	}
	
	public int getTextureCount()
	{
		return bitmapStrings.size();
	}
	
	public int getTextureWidth()
	{
		return textureWidth;
	}
	
	public int getTextureHeight()
	{
		return textureHeight;
	}
}
